window.onload = function () {
  let currentPageIndex = 0;//定义初始化的当前页索引
  let timesTemp = 0;//用于处理滚轮事件重复执行的变量
  let Opages = document.querySelectorAll(".swiper > .swiper-item");//滚动页
  let OpageSwiper = document.getElementsByClassName("swiper")[0];
  let footHeight = document.getElementsByClassName("footer")[0].clientHeight;
  let liList = $(".fp-nav ul li");

  //添加鼠标滚轮监听
  const addMouseWheeListen = () => {
    if (document.addEventListener) {
      document.addEventListener('DOMMouseScroll', handleMouseWheel, false);
    }//W3C
    window.onmousewheel = document.onmousewheel = handleMouseWheel;//IE/Opera/Chrome
  };

  let scrollToPage = () => {
    for(let i in liList) {
      liList.eq(i).find("a").removeClass("fp-nav-active");
    }
    liList.eq(currentPageIndex).find("a").addClass("fp-nav-active");
    if (currentPageIndex < Opages.length) {
      OpageSwiper.style.top = `${-(currentPageIndex) * 100}vh`;
      OpageSwiper.style.transform = `translateY(0)`;
    } else if (currentPageIndex === Opages.length) {
      OpageSwiper.style.top = `${-(currentPageIndex - 1) * 100}vh`;
      OpageSwiper.style.transform = `translateY(-${footHeight}px)`;
    }
  };
  let handleMouseWheel = (e) => {
    e = e || window.event;
    e.defaultPrevented;
    let date = new Date().getTime();
    if (date - timesTemp > 500) {
      if (e.wheelDelta) {//IE/Opera/Chrome
        if (e.wheelDelta < 0) {
          //  如果是负数，则鼠标往下滚
          if (currentPageIndex < Opages.length) {
            currentPageIndex++;
            scrollToPage();
          }
        } else if (e.wheelDelta > 0) {
          //  如果是负数，则鼠标往上滚
          if (currentPageIndex <= Opages.length && currentPageIndex > 0) {
            currentPageIndex--;
            scrollToPage();
          }
        }
      } else if (e.detail) {//Firefox
        if (e.detail > 0) {
          //  如果是负数，则鼠标往下滚
          if (currentPageIndex < Opages.length) {
            currentPageIndex++;
            scrollToPage();
          }
        } else if (e.detail < 0) {
          //  如果是负数，则鼠标往上滚
          if (currentPageIndex <= Opages.length && currentPageIndex > 0) {
            currentPageIndex--;
            scrollToPage();
          }
        }
      }
      timesTemp = date;
    }
  };

  liList.click(function () {
    let index = $(this).index();
    currentPageIndex = index;
    scrollToPage();
  })

  addMouseWheeListen();
}